<?php 
class WeixinModel extends BaseModel {

	public function get_reply(){
		$cid = $_SESSION['company']['cid'];
		$reply = D('weixin_reply')
					->join(C('DB_PREFIX').'weixin_reply_word ON '.C('DB_PREFIX').'weixin_reply.reply_id = '.C('DB_PREFIX').'weixin_reply_word.rid')
					->where(C('DB_PREFIX').'weixin_reply.is_del = 0 AND '.C('DB_PREFIX').'weixin_reply.cid = '.$cid)
					->findpage(20);
		$types = C('REPLY_TYPE');
		foreach($reply['data'] as $k => &$v){
			$v['typename'] = $types[$v['type']];
		}
		return $reply;
	}
	
	public function get_reply_image($reply_id){
		$cid = $_SESSION['company']['cid'];
		$reply = D('weixin_reply')->where('reply_id = '.$reply_id)->find();
		$reply_image = D('weixin_reply_image')->where('reply_id = '.$reply_id)->find();
		$return = array(
						'reply_id' => $reply_id,
						'cid' => $cid,
						'key' => $reply['key'],
						'fuzzy' => $reply['fuzzy'],
						'type' => $reply_image['type'],
						'content' => unserialize($reply_image['content']),
						'content_more' => unserialize($reply_image['content_more'])
					);
		$return['content']['pic'] = get_img_url($return['content']['picaid']);
		$return['content']['content'] = stripcslashes($return['content']['content']);
		if(!empty($return['content_more'])){
			$return['length'] = $reply_image['length']-1;
			foreach($return['content_more'] as $k => &$v){
				$v['pic'] = get_img_url($v['picaid']);
				if($k==1){
					$v['offset'] = 228;
				}else{
					$v['offset'] = ($k-1)*103+228;
				}
				$v['content'] = stripcslashes($v['content']);
			}
			//多图文预留位置
		}
		return $return;
	}
	
	public function add_reply_word($data){
		$data['cid'] = $_SESSION['company']['cid'];
		$data['type'] = 1;
		$data['updateline'] = time();
		$data['rid'] = D('weixin_reply')->add($data);
		if($data['rid']){
			$result = D('weixin_reply_word')->add($data);
		}
		if($result){
			return true;
		}else{
			return false;
		}
	}
	
	public function edit_reply_word($data,$reply_id){
		$map['type'] = 1;
		$map['key'] = $data['key'];
		$map['updateline'] = time();
		$result1 = D('weixin_reply')->where('reply_id = '.$reply_id)->save($map);
		
		$da['content'] = $data['content'];
		$da['updateline'] = time();
		$da['rid'] = $reply_id;
		$re = D('weixin_reply_word')->where('rid = '.$reply_id)->find();
		if(!$re){
			$result = D('weixin_reply_word')->add($da);
		}else{
			$result = D('weixin_reply_word')->where('rid = '.$reply_id)->save($da);
		}
		
		
		if($result){
			return true;
		}else{
			return false;
		}
	}
	
	public function add_reply_image_simple($data){
		$data['fuzzy'] = empty($data['fuzzy'])?0:$data['fuzzy'];
		$reply = array(
					'cid' => $_SESSION['company']['cid'],
					'key' => $data['key'],
					'type' => 2,
					'fuzzy' => $data['fuzzy'],
					'updateline' => time()
				);
		$reply_id = D('weixin_reply')->add($reply);
		$content = array(
						'title' => $data['title'],
						'author' => $data['author'],
						'description' => $data['description'],
						'picaid' => $data['picaid'],
						'content' => $data['content'],
						'url' => $data['url']
					);
		$reply_image = array(
							'reply_id' => $reply_id,
							'type' => 1,
							'length' => 1,
							'content' => serialize($content),
							'updateline' => time()
						);
		$reply_image_id = D('weixin_reply_image')->add($reply_image);
		if($reply_image_id&&$reply_id){
			return true;
		}else{
			return false;
		}
	}

	public function save_reply_image_simple($data,$reply_id){
		$data['fuzzy'] = empty($data['fuzzy'])?0:$data['fuzzy'];
		$reply = array(
					'key' => $data['key'],
					'fuzzy' => $data['fuzzy'],
					'updateline' => time()
				);
		$result = D('weixin_reply')->where('reply_id = '.$reply_id)->save($reply);
		$content = array(
						'title' => $data['title'],
						'author' => $data['author'],
						'description' => $data['description'],
						'picaid' => $data['picaid'],
						'content' => $data['content'],
						'url' => $data['url']
					);
		$reply_image = array(
							'content' => serialize($content),
							'updateline' => time()
						);
		$reply_image_id = D('weixin_reply_image')->where('reply_id = '.$reply_id)->save($reply_image);
		if($reply_image_id&&$result){
			return true;
		}else{
			return false;
		}
	}
	
	public function add_reply_image_more($data){
		$data['fuzzy'] = empty($data['fuzzy'])?0:$data['fuzzy'];
		$reply = array(
					'key' => $data['key'],
					'type' => 2,
					'fuzzy' => $data['fuzzy'],
					'updateline' => time()
				);
		$reply_id = D('weixin_reply')->add($reply);
		$reply_image = array(
							'reply_id' => $reply_id,
							'type' => 2,
							'length' => count($data['content']['mini'])+1,
							'content' => serialize($data['content']['top']),
							'content_more' => serialize($data['content']['mini']),
							'updateline' => time()
						);
		$reply_image_id = D('weixin_reply_image')->add($reply_image);
		if($reply_image_id&&$reply_id){
			return true;
		}else{
			return false;
		}
	}

	public function save_reply_image_more($data,$reply_id){
		$data['fuzzy'] = empty($data['fuzzy'])?0:$data['fuzzy'];
		$reply = array(
					'key' => $data['key'],
					'fuzzy' => $data['fuzzy'],
					'updateline' => time()
				);
		$result = D('weixin_reply')->where('reply_id = '.$reply_id)->save($reply);
		$reply_image = array(
							'length' => count($data['content']['mini'])+1,
							'content' => serialize($data['content']['top']),
							'content_more' => serialize($data['content']['mini']),
							'updateline' => time()
						);
		$reply_image_id = D('weixin_reply_image')->where('reply_id = '.$reply_id)->save($reply_image);
		if($reply_image_id&&$result){
			return true;
		}else{
			return false;
		}
	}
	
	public function get_menu_list(){
		$cid = $_SESSION['company']['cid'];
		$menus = D('custommenu')->where('is_del = 0 AND cid = '.$cid)->select();
		foreach($menus as $k => $v){
			if($v['fid'] == 0){
				$menu[$k]['fmenu'] = array(
									'mid' => $v['mid'],
									'menu' => $v['menu'],
									'value' => $v['value'],
									'type' => $v['type']
								);
				foreach($menus as $a => $b){
					if($b['fid'] == $v['mid']){
						$menu[$k]['smenu'][] = $b;
					}
				}
			}
		}
		return $menu;
	}
	
	public function get_menu($mid){
		$cid = $_SESSION['company']['cid'];
		$menu = D('custommenu')->where('mid = '.$mid.' AND cid = '.$cid)->find();
		return $menu;
	}
	
	public function get_menu_byfid($fid){
		$cid = $_SESSION['company']['cid'];
		$menu = D('custommenu')->where('fid = '.$fid.' AND is_del = 0 AND cid = '.$cid)->select();
		return $menu;
	}
	
	public function add_menu($data){
		$data['type'] = 1;
		$data['is_del'] = 0;
		$data['cid'] = $_SESSION['company']['cid'];
		$data['updateline'] = time();
		$result = D('custommenu')->add($data);
		if($result){
			return true;
		}else{
			return false;
		}
	}
	
	public function save_menu($data){
		$data['updateline'] = time();
		$result = D('custommenu')->where('mid = '.$data['mid'])->save($data);
		if($result){
			return true;
		}else{
			return false;
		}
	}
	
	public function check_menu(){
		$menu = $this->get_menu_list();
		foreach($menu as $k => $v){
			if(!empty($v['smenu'])){
				foreach($v['smenu'] as $a => $b){
					if($b['value']==''){
						return false;
					}
				}
			}else{
				if($v['fmenu']['value']==''){
					return false;
				}
			}
		}
		return true;
	}
	
	public function push_menu($menu){
		$check = $this->check_menu();
		if(!$check){
			return 3;
		}
		$cid = $_SESSION['company']['cid'];
		$company_wx = D('company_wx')->where('cid = '.$cid)->find();
		define('WECHAT_APPID',$company_wx['wx_appid']);
		define('WECHAT_APPSECRET',$company_wx['wx_appkey']);
		import('ORG.Util.weixin');
		$push = new Menu();
		$result = $push->setMenu($menu);
		if($result){
			return 1;
		}else{
			return 2;
		}
	}
	
	public function save_menu_data($data){
		$data['updateline'] = time();
		if($data['type']==1){
			$data['value'] = $data['key_value'];
		}else{
			$data['value'] = $data['url_value'];
		}
		$result = D('custommenu')->where('mid = '.$data['mid'])->save($data);
		if($result){
			return true;
		}else{
			return false;
		}
	}
	
	public function del_menu($mid){
		$data['is_del'] = 1;
		$result = D('custommenu')->where('mid = '.$mid)->save($data);
		if($result){
			return true;
		}else{
			return false;
		}
	}
	
	public function get_wxbind_qrcode(){
		$this->cleanqrcode();
		$cid = $_SESSION['company']['cid'];
		$noidentify = D('weixin_bind')->where('isidentify = 0 AND cid = '.$cid)->order('createline DESC')->find();
		$outtime = time()-$noidentify['createline'];
		if(empty($noidentify)||($outtime>1800)){
			define('WECHAT_APPID',C('WX_APPID'));
			define('WECHAT_APPSECRET',C('WX_APPKEY'));
			import('ORG.Util.weixin');
			$wxqrcode = new Popularize();
			$vcode = rand(100000,999999);
			$file = './data/temp_qrcode/wx_'.$vcode.'.png';
			$data = array(
						'cid' => $cid,
						'path' => $file,
						'code' => $vcode,
						'createline' => time()
					);
			D('weixin_bind')->add($data);
			$ticket = $wxqrcode->createTicket(1,1800,$vcode);
			$result = $wxqrcode->getQrcode($ticket['ticket'],$file);
		}else{
			$file = $noidentify['path'];
		}
		return $file;
	}
	
	public function get_accesswx(){
		$cid = $_SESSION['company']['cid'];
		$company_info = D('company_wx')->where('cid = '.$cid)->find();
		return $company_info;
	}
	
	public function save_accesswx($data){
		$cid = $_SESSION['company']['cid'];
		$data['updateline'] = time();
		$accesswx = $this->get_accesswx();
		if(empty($accesswx)){
			$data['cid'] = $cid;
			$result = D('company_wx')->add($data);
		}else{
			$result = D('company_wx')->where('cid = '.$cid)->save($data);
		}
		if($result){
			return true;
		}else{
			return false;
		}
	}
	
	public function get_wxbind_result($code){
		$qrcodeinfo = D('weixin_bind')->where('code = "'.$code.'"')->find();
		if($qrcodeinfo['isidentify']){
			$result = array('status' => 1,'bid' => $qrcodeinfo['manageid']);
		}else{
			$result = array('status' => 0,'bid' => 0);
		}
		return $result;
	}
	
	public function save_wxbind($data){
		$result = D('company_manage')->where('bid = '.$data['bid'])->save($data);
		if($result){
			$bindinfo = D('company_manage')->where('bid = '.$data['bid'])->find();
			$data = array(
						'first' => array('value'=>'您已经成功绑定'.$_SESSION['company']['company_name'].'管理员账号','color'=>'#000'),
						'keyword1' => array('value'=>$bindinfo['wx_name'],'color'=>'#000'),
						'keyword2' => array('value'=>'你可以直接使用该微信登录人才来了「移动版」','color'=>'#000'),
						'remark' => array('value'=>'如需解绑，请访问桌面版进行操作','color'=>'#000')
					);
			weixinmessage('wxbind',$data,$bindinfo['wx_id'],'#42bbc3');
			return true;
		}else{
			return false;
		}
	}
	
	public function del_wxbind($bid){
		$data['is_del'] = 1;
		$result = D('company_manage')->where('bid = '.$bid)->save($data);
		if($result){
			$bindinfo = D('company_manage')->where('bid = '.$bid)->find();
			$data = array(
						'first' => array('value'=>'您已经成功与'.$_SESSION['company']['company_name'].'解除绑定','color'=>'#000'),
						'keyword1' => array('value'=>$bindinfo['wx_name'],'color'=>'#000'),
						'keyword2' => array('value'=>'该微信已不能登录人才来了「移动版」','color'=>'#000'),
						'remark' => array('value'=>'如需绑定其他帐号，请访问桌面版进行操作','color'=>'#000')
					);
			weixinmessage('wxdebind',$data,$bindinfo['wx_id'],'#42bbc3');
			return true;
		}else{
			return false;
		}
	}
	
	public function cleanqrcode(){
		$qrcode = D('weixin_bind')->select();
		foreach($qrcode as $k => $v){
			$outtime = time()-$v['createline'];
			if($outtime>1800){
				unlink($v['path']);
				$out[]=$v['bid'];
			}
		}
		D('weixin_bind')->where('bid IN('.implode(',',$out).')')->delete();
	}

}
?>